package jsyf.oa.service.returnVisit;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.returnVisit.ReturnVisitDao;
import jsyf.oa.entity.returnVisit.ReturnVisitEntity;
import jsyf.oa.facade.returnVisit.ReturnVisitFacade;
import jsyf.oa.service.report.reportCommon.ReportCommonMethodsAndConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 92852 on 2018/6/27.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class ReturnVisitService implements ReturnVisitFacade {
    @Autowired
    private ReturnVisitDao returnVisitDao;
    @Autowired
    private ReportCommonMethodsAndConstant common;

    @Override
    public JSONObject save(String jsonData) {
        ReturnVisitEntity returnVisitEntity = JSON.toJavaObject(JSON.parseObject(jsonData), ReturnVisitEntity.class);
        Integer rows = 0;
        JSONObject obj = new JSONObject();
        if ("0".equals(returnVisitEntity.getId()) || StringUtils.isBlank(returnVisitEntity.getId())) {
            rows = returnVisitDao.add(returnVisitEntity);
        } else {
            rows = returnVisitDao.update(returnVisitEntity);
        }
        if (rows > 0) {
            obj.put("records", "保存成功");
        } else {
            obj.put("records", "保存失败");
        }
        return obj;
    }

    @Override
    public JSONObject getListInfo(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        Integer spacing = null;
        int length = dateTime.length();
        String dateCut = null;
        List<JSONObject> records = null;
        /*分页信息*/
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        StringBuffer condition = new StringBuffer();
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }

        if (dateTime.equals("today")) {
            /*今日*/
            condition.append(" AND( \n" +
                    " ( TO_DAYS(NOW())-TO_DAYS(t_sys_user.ENTRY_DATE)=7)\n" +
                    " OR ( TO_DAYS(NOW())-TO_DAYS(t_sys_user.ENTRY_DATE)=30)\n" +
                    " OR (TO_DAYS(NOW())-TO_DAYS(t_sys_user.ENTRY_DATE)=60)\n" +
                    " OR (TO_DAYS(NOW())-TO_DAYS(t_sys_user_leave.LEAVE_DATE)=1)\n" +
                    ")"+"\n");
            records = returnVisitDao.getListInfo(condition.toString(),requestPage, pageSize);
        } else if (dateTime.equals("all")) {
            /*全部*/
            condition.append(" AND t_sys_return_visit.CREATE_TIME like"+" '"+"%%"+"'"+"\n");
            condition.append(" AND t_sys_return_visit.`STATUS`=1"+"\n");
            records = returnVisitDao.getListInfo(condition.toString(),requestPage, pageSize);

        } else {
            /*距离周一的天数*/
            if (dateTime.length() == common.NUMBER_TWELVE) {
                spacing = common.getWeekDay(dateTime);
            }
            switch (length) {
                case 7:
                    condition.append(" AND t_sys_return_visit.CREATE_TIME LIKE" + "'" + dateTime + "%'" + "\n");
                    break;
                case 12:
                    dateCut = dateTime.substring(0, 10);
                    condition.append(" AND '" + dateCut + "'>=date(t_sys_return_visit.CREATE_TIME) \n");
                    condition.append(" AND DATE_SUB('" + dateCut + "', INTERVAL " + spacing + " DAY) <= date(t_sys_return_visit.CREATE_TIME) \n");
                    break;
            }
            condition.append(" AND t_sys_return_visit.`STATUS`=1"+"\n");
            records = returnVisitDao.getListInfo(condition.toString(),requestPage, pageSize);
        }
        int recordCount = records.size();
        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int pageCount = recordCount % pageSize == 0 ? recordCount / pageSize : (recordCount / pageSize) + 1;
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    @Override
    public JSONObject getPersonalByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        StringBuffer condition = new StringBuffer();
        if (!StringUtils.isBlank(userId)) {
            condition.append("AND t_sys_user.ID=" + " '" + userId + "'" + "\n");
        }
        JSONObject records=returnVisitDao.getPersonalByUserId(condition.toString(),userId);
        return records;
    }


}
